/*
 * $Id: Path.h,v 1.5 2005-06-28 10:53:20 bacon Exp $
 */

#ifndef _XPP_SYS_PATH_CLASS_
#define _XPP_SYS_PATH_CLASS_

#include <xp/types.h>
#include <xp/macros.h>
#include <xp/bas/sysapi.h>
#include <xp/bas/string.h>

namespace xpp
{
	namespace sys
	{

		class Path
		{
		public:
			enum
			{
				MAX_LEN = XP_PATH_MAX
			};

			Path ();
			Path (const xp_char_t* n);
			Path (const Path& fn);
			Path& operator= (const Path& fn);
		
			const xp_char_t* name () const 
			{
				return full_path;
			}

			void setName (const xp_char_t* n) 
			{
				if (n == XP_NULL || n[0] == XP_CHAR('\0')) set_to_root();
				else {
					xp_strxcpy (
						full_path, xp_countof(full_path), n);
					set_base_name ();
				}
			}
		
			const xp_char_t* baseName () const 
			{
				return base_name;
			}
			const xp_char_t* baseDir () const 
			{
				return base_dir;
			}
		
			bool exists () 
			{
				return exists (full_path);
			}
			static bool exists (const xp_char_t* path);
		
			int  getSize        (xp_off_t* sz);
			bool isWritable     ();
			bool isReadable     ();
			bool isReadWritable ();
		#ifndef _WIN32
			bool isExecutable   ();
		#endif
		
			bool isDirectory () const
			{
				return isDirectory (full_path);
			}
			static bool isDirectory (const xp_char_t* path);
		
			bool isRegular () const
			{
				return isRegular (full_path);
			}
			static bool isRegular (const xp_char_t* path);
		
			int chmod (xp_mode_t mode);
			static int chmod (const xp_char_t* path, xp_mode_t mode);

			int unlink ();
			static int unlink (const xp_char_t* path);

			int mkdir (xp_mode_t mode);
			static int mkdir (const xp_char_t* path, xp_mode_t mode);
		
			int setToSelf (const xp_char_t* argv0 = XP_NULL);
		
		protected:
			xp_char_t full_path[XP_PATH_MAX + 1];
			xp_char_t base_name[XP_PATH_MAX + 1];
			xp_char_t base_dir [XP_PATH_MAX + 1];
		
			void set_base_name ();
			void set_to_root ();
		};

	}
}
		
#endif
